package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.utils.Constants;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveHashCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CommentService {

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 查询动态评论列表
     * @param page
     * @param pagesize
     * @param movementId
     * @return
     */
    public PageResult comments(int page, int pagesize, String movementId) {
        //调用Api
        List<Comment> commentList = commentApi.comments(page, pagesize, movementId, CommentType.COMMENT);

        if (CollUtil.isEmpty(commentList)){
            return new PageResult();
        }

        List<Long> userIds = commentList.stream().map(item -> item.getUserId()).collect(Collectors.toList());

        List<CommentVo> list = new ArrayList<>();

        Map<Long, UserInfo> map = userInfoApi.findByIds(new UserInfo(), userIds);
        for (Comment comment : commentList) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo != null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                //判断redis中是否有点赞标记
                if (redisTemplate.opsForValue().get(Constants.COMMENT_LIKE_KEY+comment.getId().toHexString()) != null){
                    vo.setHasLiked(1);
                }
                list.add(vo);
            }
        }
        return new PageResult(page,pagesize,commentList.size(),list);
    }

    /**
     * 发布评论
     * @param movementId
     * @param content
     */
    public void saveComments(String movementId, String content) {
        //调用API获取当前评论动态的用户id
        //Long publishUserId =movementApi.getByUserId(movementId);
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setPublishId(new ObjectId(movementId));
        comment.setCommentType(CommentType.COMMENT.getType());
        comment.setUserId(UserHolder.getUserId());
        comment.setCreated(System.currentTimeMillis());

        //调用API保存发布的评论,并获取评论总数
        Integer countComments = commentApi.saveComments(comment);
    }

    /**
     * 评论点赞
     * @param commentId
     * @return
     */
    public Integer commentLike(String commentId) {
        //调用API获取评论点赞总数
        Integer count = commentApi.saveLikeCount(commentId);

        //调用redis,存入评论点赞的标记
        String key = Constants.COMMENT_LIKE_KEY + commentId;
        redisTemplate.opsForValue().set(key,UserHolder.getUserId().toString());

        return count;
    }

    /**
     * 评论取消点赞
     * @param commentId
     * @return
     */
    public Integer dislike(String commentId) {
        Integer count = commentApi.dislike(commentId);

        //删除redis中评论点赞的标记
        redisTemplate.delete(Constants.COMMENT_LIKE_KEY + commentId);
        return count;
    }
}
